package com.tony.jdbctemplate.controller;

import com.tony.jdbctemplate.entity.Student;
import com.tony.jdbctemplate.service.StudentService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * create by xuxia on 2024/2/6 16:57
 */
@RestController
@RequestMapping(value = "/index")
public class IndexController {

    @Resource
    private StudentService studentService;

    @GetMapping(value = "/addStudent")
    public String addStudent(String sno, String name, String sex) {
        Student student = new Student();
        student.setSno(sno);
        student.setName(name);
        student.setSex(sex);
        int i = studentService.addStudent(student);
        return i == 1 ? "新增成功" : "新增失败";
    }

    @GetMapping(value = "/updateStudent")
    public String updateStudent(String sno, String name) {
        Student student = studentService.queryStudent(sno);
        String msg = null;
        if (student != null) {
            student.setName(name);
            int i = studentService.updateStudent(student);
            msg = i == 1 ? "学生信息修改成功" : "学生信息修改失败";
        } else {
            msg = "该学生信息不存在";
        }
        return msg;
    }

    @GetMapping(value = "/deleteStudent")
    public String deleteStudent(String sno) {
        int i = studentService.deleteStudent(sno);
        return i == 1 ? "删除学生信息成功" : "删除学生信息失败";
    }

    @GetMapping(value = "/queryStudent")
    public Student queryStudent(String sno) {
        Student student = studentService.queryStudent(sno);
        return student;
    }

    @GetMapping(value = "/queryAllStudentListMap")
    public List<Map<String, Object>> queryAllStudentListMap() {
        return studentService.queryAllStudentsOfMap();
    }

    @GetMapping(value = "/queryAllStudents")
    public List<Student> queryAllStudents() {
        return studentService.queryAllStudentsOfBean();
    }
}
